﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Hangjing.Model;
using Hangjing.DBUtility;
using System.Collections;
using Hangjing.Common;
//using Hangjing.IDAL;


namespace Hangjing.SQLServerDAL
{
    public class Deliver
    {

        /// <summary>
        /// 统计
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public DeliverInfo GetCurrentMounthSum(string where)
        {
            string sql = "SELECT SUM(delivergetfee_base) AS shopdiscountmoney, SUM(DeliverGetSendFee) AS reveint3 FROM dbo.Custorder LEFT JOIN dbo.Deliver ON dbo.Deliver.DataId = dbo.Custorder.deliverid  ";
            sql += "  where " + where;

            DeliverInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    model = new DeliverInfo();

                    model.reveint3 = HJConvert.ToDecimal(dr["reveint3"]);
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);


                }
            }
            return model;
        }

        /// <summary>
        /// 兼职，全职统计
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public DeliverInfo GetPostSum(string where)
        {
            string sql = "select ";
            sql += "  sum (reveint2)  as reveint2";
            sql += " , sum (case when post = 100 then(1) else 0   end)  as fulltimedeliver";
            sql += " , sum (case when post = 90 then(1) else 0   end)  as parttimedeliver";

            sql += " from Deliver  where " + where;

            DeliverInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    model = new DeliverInfo();
                    model.reveint2 = HJConvert.ToInt32(dr["reveint2"]);
                    model.UserName = HJConvert.ToString(dr["fulltimedeliver"]);
                    model.Password = HJConvert.ToString(dr["parttimedeliver"]);

                }
            }
            return model;
        }

        /// <summary>
        /// 配送员导出数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public IList<DeliverInfo> export2ExcelData(string strWhere)
        {
            IList<DeliverInfo> infos = new List<DeliverInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@where", SqlDbType.VarChar,1500),
            };
            parameters[0].Value = strWhere;


            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "Deliver_export2Excel", parameters))
            {
                while (dr.Read())
                {
                    DeliverInfo model = new DeliverInfo();
                    model.DataId = HJConvert.ToInt32(dr["DataId"]);
                    model.CodeId = HJConvert.ToString(dr["CodeId"]);
                    model.Name = HJConvert.ToString(dr["Name"]);
                    model.Phone = HJConvert.ToString(dr["Phone"]);
                    model.Section = HJConvert.ToString(dr["Section"]);
                    model.Status = HJConvert.ToInt32(dr["Status"]);
                    model.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    model.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    model.UserName = HJConvert.ToString(dr["UserName"]);
                    model.Password = HJConvert.ToString(dr["Password"]);
                    model.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    model.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    model.AddDate = HJConvert.ToDateTime(dr["AddDate"]);
                    model.sessionid = HJConvert.ToString(dr["sessionid"]);
                    model.workstate = HJConvert.ToInt32(dr["workstate"]);
                    model.cartype = HJConvert.ToString(dr["cartype"]);
                    model.carower = HJConvert.ToInt32(dr["carower"]);
                    model.carnum = HJConvert.ToString(dr["carnum"]);
                    model.post = HJConvert.ToInt32(dr["post"]);
                    model.grade = HJConvert.ToInt32(dr["grade"]);
                    model.reveint1 = HJConvert.ToInt32(dr["reveint1"]);
                    model.reveint2 = HJConvert.ToInt32(dr["reveint2"]);
                    model.reveint3 = HJConvert.ToDecimal(dr["reveint3"]);
                    model.revevar1 = HJConvert.ToString(dr["revevar1"]);
                    model.revevar2 = HJConvert.ToString(dr["revevar2"]);
                    model.revevar3 = HJConvert.ToString(dr["revevar3"]);
                    model.revefloat1 = HJConvert.ToDecimal(dr["revefloat1"]);
                    model.revefloat2 = HJConvert.ToDecimal(dr["revefloat2"]);
                    model.revedate = HJConvert.ToDateTime(dr["revedate"]);
                    model.Groupname = HJConvert.ToString(dr["Groupname"]);
                    model.ordercount = HJConvert.ToInt32(dr["ordercount"]);
                    model.cardpay = HJConvert.ToDecimal(dr["cardpay"]);

                    model.detailinfo = new deliverInformationInfo();
                    model.detailinfo.address = HJConvert.ToString(dr["address"]);
                    model.detailinfo.Marriage = HJConvert.ToString(dr["Marriage"]);
                    model.detailinfo.homeaddress = HJConvert.ToString(dr["homeaddress"]);
                    model.detailinfo.Nation = HJConvert.ToString(dr["Nation"]);
                    model.detailinfo.QQ = HJConvert.ToString(dr["QQ"]);
                    model.detailinfo.Education = HJConvert.ToString(dr["Education"]);
                    model.detailinfo.Emergency = HJConvert.ToString(dr["Emergency"]);
                    model.detailinfo.Emergencyphone = HJConvert.ToString(dr["Emergencyphone"]);
                    model.detailinfo.Entrytime = HJConvert.ToString(dr["Entrytime"]);
                    model.detailinfo.Salarycard = HJConvert.ToString(dr["Salarycard"]);
                    model.detailinfo.Bank = HJConvert.ToString(dr["Bank"]);
                    model.detailinfo.workexperience = HJConvert.ToString(dr["workexperience"]);
                    model.detailinfo.Reward = HJConvert.ToString(dr["Reward"]);
                    model.detailinfo.Complaint = HJConvert.ToString(dr["Complaint"]);
                    model.detailinfo.Bond = HJConvert.ToDecimal(dr["Bond"]);
                    model.detailinfo.age = HJConvert.ToInt32(dr["age"]);
                    model.detailinfo.revevar1 = HJConvert.ToString(dr["identitycard"]);

                    model.detailinfo.remark = HJConvert.ToString(dr["remark"]);

                    infos.Add(model);
                }
            }
            return infos;
        }

        /// <summary>
        /// 当月配送员薪资结算
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <param name="strWhere"></param>
        /// <param name="orderwhere"></param>
        /// <returns></returns>
        public IList<DeliverInfo> CurrentMounthSalarySum(int pagesize, int pageindex, string strWhere, string orderwhere, string ordername)
        {
            IList<DeliverInfo> infos = new List<DeliverInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@PageSize", SqlDbType.Int),
                new SqlParameter("@PageIndex", SqlDbType.Int),
                new SqlParameter("@strWhere", SqlDbType.VarChar,1500),
                new SqlParameter("@orderwhere", SqlDbType.VarChar,1500),
                new SqlParameter("@ordername", SqlDbType.VarChar,100)
            };
            parameters[0].Value = pagesize;
            parameters[1].Value = pageindex;
            parameters[2].Value = strWhere;
            parameters[3].Value = orderwhere;
            parameters[4].Value = ordername;


            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "deliver_CurrentMounthSalarySum", parameters))
            {
                while (dr.Read())
                {
                    DeliverInfo model = new DeliverInfo();
                    model.DataId = HJConvert.ToInt32(dr["DataId"]);
                    model.CodeId = HJConvert.ToString(dr["CodeId"]);
                    model.Name = HJConvert.ToString(dr["Name"]);
                    model.Phone = HJConvert.ToString(dr["Phone"]);
                    model.Section = HJConvert.ToString(dr["Section"]);
                    model.Status = HJConvert.ToInt32(dr["Status"]);
                    model.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    model.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    model.UserName = HJConvert.ToString(dr["UserName"]);
                    model.Password = HJConvert.ToString(dr["Password"]);
                    model.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    model.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    model.AddDate = HJConvert.ToDateTime(dr["AddDate"]);
                    model.sessionid = HJConvert.ToString(dr["sessionid"]);
                    model.workstate = HJConvert.ToInt32(dr["workstate"]);
                    model.cartype = HJConvert.ToString(dr["cartype"]);
                    model.carower = HJConvert.ToInt32(dr["carower"]);
                    model.carnum = HJConvert.ToString(dr["carnum"]);
                    model.post = HJConvert.ToInt32(dr["post"]);
                    model.grade = HJConvert.ToInt32(dr["grade"]);
                    model.reveint1 = HJConvert.ToInt32(dr["reveint1"]);

                    model.revevar1 = HJConvert.ToString(dr["revevar1"]);
                    model.revevar2 = HJConvert.ToString(dr["revevar2"]);
                    model.revevar3 = HJConvert.ToString(dr["revevar3"]);
                    model.revefloat1 = HJConvert.ToDecimal(dr["revefloat1"]);
                    model.revefloat2 = HJConvert.ToDecimal(dr["revefloat2"]);

                    model.revedate = HJConvert.ToDateTime(dr["revedate"]);
                    model.recordtcount = HJConvert.ToInt32(dr["recordtcount"]);
                    model.cityname = HJConvert.ToString(dr["cityname"]);
                    model.Groupname = HJConvert.ToString(dr["Groupname"]);


                    model.cut = HJConvert.ToDecimal(dr["s_cut"]);
                    model.salary = HJConvert.ToDecimal(dr["s_salary"]);
                    model.deliverprofit = HJConvert.ToDecimal(dr["s_deliverprofit"]);
                    model.reveint2 = HJConvert.ToInt32(dr["basesalary"]);
                    model.reveint3 = HJConvert.ToDecimal(dr["percentagerat"]);
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);


                    infos.Add(model);
                }
            }
            return infos;
        }


        /// <summary>
        /// 配送员薪资结算
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <param name="strWhere"></param>
        /// <param name="orderwhere"></param>
        /// <returns></returns>
        public IList<DeliverInfo> GetListWithOrderStatistics(int pagesize, int pageindex, string strWhere, string orderwhere, string ordername)
        {
            IList<DeliverInfo> infos = new List<DeliverInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@PageSize", SqlDbType.Int),
                new SqlParameter("@PageIndex", SqlDbType.Int),
                new SqlParameter("@strWhere", SqlDbType.VarChar,1500),
                new SqlParameter("@orderwhere", SqlDbType.VarChar,1500),
                new SqlParameter("@ordername", SqlDbType.VarChar,100)
            };
            parameters[0].Value = pagesize;
            parameters[1].Value = pageindex;
            parameters[2].Value = strWhere;
            parameters[3].Value = orderwhere;
            parameters[4].Value = ordername;


            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "deliver_GetListWithOrderStatistics", parameters))
            {
                while (dr.Read())
                {
                    DeliverInfo model = new DeliverInfo();
                    model.DataId = HJConvert.ToInt32(dr["DataId"]);
                    model.CodeId = HJConvert.ToString(dr["CodeId"]);
                    model.Name = HJConvert.ToString(dr["Name"]);
                    model.Phone = HJConvert.ToString(dr["Phone"]);
                    model.Section = HJConvert.ToString(dr["Section"]);
                    model.Status = HJConvert.ToInt32(dr["Status"]);
                    model.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    model.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    model.UserName = HJConvert.ToString(dr["UserName"]);
                    model.Password = HJConvert.ToString(dr["Password"]);
                    model.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    model.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    model.AddDate = HJConvert.ToDateTime(dr["AddDate"]);
                    model.sessionid = HJConvert.ToString(dr["sessionid"]);
                    model.workstate = HJConvert.ToInt32(dr["workstate"]);
                    model.cartype = HJConvert.ToString(dr["cartype"]);
                    model.carower = HJConvert.ToInt32(dr["carower"]);
                    model.carnum = HJConvert.ToString(dr["carnum"]);
                    model.post = HJConvert.ToInt32(dr["post"]);
                    model.grade = HJConvert.ToInt32(dr["grade"]);
                    model.reveint1 = HJConvert.ToInt32(dr["reveint1"]);

                    model.revevar1 = HJConvert.ToString(dr["revevar1"]);
                    model.revevar2 = HJConvert.ToString(dr["revevar2"]);
                    model.revevar3 = HJConvert.ToString(dr["revevar3"]);
                    model.revefloat1 = HJConvert.ToDecimal(dr["revefloat1"]);
                    model.revefloat2 = HJConvert.ToDecimal(dr["revefloat2"]);

                    model.revedate = HJConvert.ToDateTime(dr["revedate"]);
                    model.recordtcount = HJConvert.ToInt32(dr["recordtcount"]);
                    model.cityname = HJConvert.ToString(dr["cityname"]);
                    model.Groupname = HJConvert.ToString(dr["Groupname"]);


                    model.cut = HJConvert.ToDecimal(dr["s_cut"]);
                    model.salary = HJConvert.ToDecimal(dr["s_salary"]);
                    model.deliverprofit = HJConvert.ToDecimal(dr["s_deliverprofit"]);
                    model.reveint2 = HJConvert.ToInt32(dr["basesalary"]);
                    model.reveint3 = HJConvert.ToDecimal(dr["percentagerat"]);
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);


                    infos.Add(model);
                }
            }
            return infos;
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Hangjing.Model.DeliverInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Deliver(");
            strSql.Append("CodeId,Name,Phone,Section,Status,GpsIMEI,OrderNum,UserName,Password,Inve1,Inve2,AddDate,sessionid,workstate,cartype,carower,carnum,post,grade,reveint1,reveint2,reveint3,revevar1,revevar2,revevar3,revefloat1,revefloat2,revedate,usermoney,reveint4,reveint5,reveint6,revevar4,revevar5,revefloat3,revefloat4)");
            strSql.Append(" values (");
            strSql.Append("@CodeId,@Name,@Phone,@Section,@Status,@GpsIMEI,@OrderNum,@UserName,@Password,@Inve1,@Inve2,@AddDate,@sessionid,@workstate,@cartype,@carower,@carnum,@post,@grade,@reveint1,@reveint2,@reveint3,@revevar1,@revevar2,@revevar3,@revefloat1,@revefloat2,@revedate,@usermoney,@reveint4,@reveint5,@reveint6,@revevar4,@revevar5,@revefloat3,@revefloat4)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                new SqlParameter("@CodeId", SqlDbType.VarChar,10),
                new SqlParameter("@Name", SqlDbType.VarChar,50),
                new SqlParameter("@Phone", SqlDbType.VarChar,50),
                new SqlParameter("@Section", SqlDbType.VarChar,256),
                new SqlParameter("@Status", SqlDbType.Int,4),
                new SqlParameter("@GpsIMEI", SqlDbType.VarChar,50),
                new SqlParameter("@OrderNum", SqlDbType.Int,4),
                new SqlParameter("@UserName", SqlDbType.NVarChar,50),
                new SqlParameter("@Password", SqlDbType.NVarChar,50),
                new SqlParameter("@Inve1", SqlDbType.Int,4),
                new SqlParameter("@Inve2", SqlDbType.VarChar,256),
                new SqlParameter("@AddDate", SqlDbType.DateTime),
                new SqlParameter("@sessionid", SqlDbType.VarChar,256) ,
                new SqlParameter("@workstate", SqlDbType.Int,4) ,
                new SqlParameter("@cartype", SqlDbType.VarChar,256) ,
                new SqlParameter("@carower", SqlDbType.Int,4) ,
                new SqlParameter("@carnum", SqlDbType.VarChar,50) ,
                new SqlParameter("@post", SqlDbType.Int,4) ,
                new SqlParameter("@grade", SqlDbType.Int,4) ,
                new SqlParameter("@reveint1", SqlDbType.Int,4) ,
                new SqlParameter("@reveint2", SqlDbType.Int,4) ,
                new SqlParameter("@reveint3", SqlDbType.Decimal) ,
                new SqlParameter("@revevar1", SqlDbType.VarChar,256) ,
                new SqlParameter("@revevar2", SqlDbType.VarChar,256) ,
                new SqlParameter("@revevar3", SqlDbType.VarChar,256) ,
                new SqlParameter("@revefloat1", SqlDbType.Decimal,5) ,
                new SqlParameter("@revefloat2", SqlDbType.Decimal,5) ,
                new SqlParameter("@revedate", SqlDbType.DateTime),

               new SqlParameter("@usermoney", SqlDbType.Decimal,5) ,
               new SqlParameter("@reveint4", SqlDbType.Int,4) ,
               new SqlParameter("@reveint5", SqlDbType.Int,4) ,
               new SqlParameter("@reveint6", SqlDbType.Int,4) ,
               new SqlParameter("@revevar4", SqlDbType.VarChar,256) ,
               new SqlParameter("@revevar5", SqlDbType.VarChar,256) ,
               new SqlParameter("@revefloat3", SqlDbType.Decimal,5) ,
               new SqlParameter("@revefloat4", SqlDbType.Decimal,5)

            };
            parameters[0].Value = model.CodeId;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Phone;
            parameters[3].Value = model.Section;
            parameters[4].Value = model.Status;
            parameters[5].Value = model.GpsIMEI;
            parameters[6].Value = model.OrderNum;
            parameters[7].Value = model.UserName;
            parameters[8].Value = model.Password;
            parameters[9].Value = model.Inve1;
            parameters[10].Value = model.Inve2;
            parameters[11].Value = model.AddDate;
            parameters[12].Value = "";
            parameters[13].Value = model.workstate;
            parameters[14].Value = model.cartype;
            parameters[15].Value = model.carower;
            parameters[16].Value = model.carnum;
            parameters[17].Value = model.post;
            parameters[18].Value = model.grade;
            parameters[19].Value = model.reveint1;
            parameters[20].Value = model.reveint2;
            parameters[21].Value = model.reveint3;
            parameters[22].Value = model.revevar1;
            parameters[23].Value = model.revevar2;
            parameters[24].Value = model.revevar3;
            parameters[25].Value = model.revefloat1;
            parameters[26].Value = model.revefloat2;
            parameters[27].Value = model.revedate;
            parameters[28].Value = model.usermoney;
            parameters[29].Value = model.reveint4;
            parameters[30].Value = model.reveint5;
            parameters[31].Value = model.reveint6;
            parameters[32].Value = model.revevar4;
            parameters[33].Value = model.revevar5;
            parameters[34].Value = model.revefloat3;
            parameters[35].Value = model.revefloat4;


            return Convert.ToInt32(SQLHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters));


        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(Hangjing.Model.DeliverInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Deliver set ");
            strSql.Append("CodeId=@CodeId,");
            strSql.Append("Name=@Name,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("Section=@Section,");
            strSql.Append("Status=@Status,");
            strSql.Append("GpsIMEI=@GpsIMEI,");
            strSql.Append("OrderNum=@OrderNum,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Password=@Password,");
            strSql.Append("Inve1=@Inve1,");
            strSql.Append("Inve2=@Inve2,");
            strSql.Append(" sessionid = @sessionid , ");
            strSql.Append(" workstate = @workstate , ");
            strSql.Append(" cartype = @cartype , ");
            strSql.Append(" carower = @carower , ");
            strSql.Append(" carnum = @carnum , ");
            strSql.Append(" post = @post , ");
            strSql.Append(" grade = @grade , ");
            strSql.Append(" reveint1 = @reveint1 , ");
            strSql.Append(" reveint2 = @reveint2 , ");
            strSql.Append(" reveint3 = @reveint3 , ");
            strSql.Append(" revevar1 = @revevar1 , ");
            strSql.Append(" revevar2 = @revevar2 , ");
            strSql.Append(" revevar3 = @revevar3 , ");
            strSql.Append(" revefloat1 = @revefloat1 , ");
            strSql.Append(" revefloat2 = @revefloat2 , ");




            strSql.Append(" usermoney = @usermoney , ");
            strSql.Append(" reveint4 = @reveint4 , ");
            strSql.Append(" reveint5 = @reveint5 , ");
            strSql.Append(" reveint6 = @reveint6 , ");
            strSql.Append(" revevar4 = @revevar4 , ");
            strSql.Append(" revevar5 = @revevar5 , ");
            strSql.Append(" revefloat3 = @revefloat3 , ");
            strSql.Append(" revefloat4 = @revefloat4 ,");


            strSql.Append(" revedate = @revedate,  ");


            strSql.Append("AddDate=@AddDate");
            strSql.Append(" where DataId=@DataId");
            SqlParameter[] parameters =
            {
                new SqlParameter("@DataId", SqlDbType.Int,4),
                new SqlParameter("@CodeId", SqlDbType.VarChar,10),
                new SqlParameter("@Name", SqlDbType.VarChar,50),
                new SqlParameter("@Phone", SqlDbType.VarChar,50),
                new SqlParameter("@Section", SqlDbType.VarChar,256),
                new SqlParameter("@Status", SqlDbType.Int,4),
                new SqlParameter("@GpsIMEI", SqlDbType.VarChar,50),
                new SqlParameter("@OrderNum", SqlDbType.Int,4),
                new SqlParameter("@UserName", SqlDbType.NVarChar,50),
                new SqlParameter("@Password", SqlDbType.NVarChar,50),
                new SqlParameter("@Inve1", SqlDbType.Int,4),
                new SqlParameter("@Inve2", SqlDbType.VarChar,256),
                new SqlParameter("@AddDate", SqlDbType.DateTime),

                new SqlParameter("@sessionid", SqlDbType.VarChar,256) ,

                new SqlParameter("@workstate", SqlDbType.Int,4) ,
                new SqlParameter("@cartype", SqlDbType.VarChar,256) ,
                new SqlParameter("@carower", SqlDbType.Int,4) ,
                new SqlParameter("@carnum", SqlDbType.VarChar,50) ,
                new SqlParameter("@post", SqlDbType.Int,4) ,
                new SqlParameter("@grade", SqlDbType.Int,4) ,
                new SqlParameter("@reveint1", SqlDbType.Int,4) ,
                new SqlParameter("@reveint2", SqlDbType.Int,4) ,
                new SqlParameter("@reveint3", SqlDbType.Decimal) ,
                new SqlParameter("@revevar1", SqlDbType.VarChar,256) ,
                new SqlParameter("@revevar2", SqlDbType.VarChar,256) ,
                new SqlParameter("@revevar3", SqlDbType.VarChar,256) ,
                new SqlParameter("@revefloat1", SqlDbType.Decimal,5) ,
                new SqlParameter("@revefloat2", SqlDbType.Decimal,5) ,
                new SqlParameter("@revedate", SqlDbType.DateTime),


                 new SqlParameter("@usermoney", SqlDbType.Decimal,5) ,
                 new SqlParameter("@reveint4", SqlDbType.Int,4) ,
                 new SqlParameter("@reveint5", SqlDbType.Int,4) ,
                 new SqlParameter("@reveint6", SqlDbType.Int,4) ,
                 new SqlParameter("@revevar4", SqlDbType.VarChar,256) ,
                 new SqlParameter("@revevar5", SqlDbType.VarChar,256) ,
                 new SqlParameter("@revefloat3", SqlDbType.Decimal,5) ,
                 new SqlParameter("@revefloat4", SqlDbType.Decimal,5)

            };
            parameters[0].Value = model.DataId;
            parameters[1].Value = model.CodeId;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Phone;
            parameters[4].Value = model.Section;
            parameters[5].Value = model.Status;
            parameters[6].Value = model.GpsIMEI;
            parameters[7].Value = model.OrderNum;
            parameters[8].Value = model.UserName;
            parameters[9].Value = model.Password;
            parameters[10].Value = model.Inve1;
            parameters[11].Value = model.Inve2;
            parameters[12].Value = model.AddDate;

            parameters[13].Value = model.sessionid;

            parameters[14].Value = model.workstate;
            parameters[15].Value = model.cartype;
            parameters[16].Value = model.carower;
            parameters[17].Value = model.carnum;
            parameters[18].Value = model.post;
            parameters[19].Value = model.grade;
            parameters[20].Value = model.reveint1;
            parameters[21].Value = model.reveint2;
            parameters[22].Value = model.reveint3;
            parameters[23].Value = model.revevar1;
            parameters[24].Value = model.revevar2;
            parameters[25].Value = model.revevar3;
            parameters[26].Value = model.revefloat1;
            parameters[27].Value = model.revefloat2;
            parameters[28].Value = model.revedate;

            parameters[29].Value = model.usermoney;
            parameters[30].Value = model.reveint4;
            parameters[31].Value = model.reveint5;
            parameters[32].Value = model.reveint6;
            parameters[33].Value = model.revevar4;
            parameters[34].Value = model.revevar5;
            parameters[35].Value = model.revefloat3;
            parameters[36].Value = model.revefloat4;



            return SQLHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取记录所有字段
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        /// <param>DataId</param>
        /// <returns>DeliverInfo</returns>
        public DeliverInfo GetModel(int DataId)
        {
            string sql = "select * from Deliver where  DataId = @DataId";
            SqlParameter parameter = new SqlParameter("@DataId", SqlDbType.Int, 4);
            parameter.Value = DataId;
            DeliverInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, parameter))
            {
                if (dr.Read())
                {
                    model = new DeliverInfo();
                    model.DataId = HJConvert.ToInt32(dr["DataId"]);
                    model.CodeId = HJConvert.ToString(dr["CodeId"]);
                    model.Name = HJConvert.ToString(dr["Name"]);
                    model.Phone = HJConvert.ToString(dr["Phone"]);
                    model.Section = HJConvert.ToString(dr["Section"]);
                    model.Status = HJConvert.ToInt32(dr["Status"]);
                    model.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    model.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    model.UserName = HJConvert.ToString(dr["UserName"]);
                    model.Password = HJConvert.ToString(dr["Password"]);
                    model.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    model.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    model.AddDate = HJConvert.ToDateTime(dr["AddDate"]);
                    model.sessionid = HJConvert.ToString(dr["sessionid"]);
                    model.workstate = HJConvert.ToInt32(dr["workstate"]);
                    model.cartype = HJConvert.ToString(dr["cartype"]);
                    model.carower = HJConvert.ToInt32(dr["carower"]);
                    model.carnum = HJConvert.ToString(dr["carnum"]);
                    model.post = HJConvert.ToInt32(dr["post"]);
                    model.grade = HJConvert.ToInt32(dr["grade"]);
                    model.reveint1 = HJConvert.ToInt32(dr["reveint1"]);
                    model.reveint2 = HJConvert.ToInt32(dr["reveint2"]);
                    model.reveint3 = HJConvert.ToDecimal(dr["reveint3"]);
                    model.revevar1 = HJConvert.ToString(dr["revevar1"]);
                    model.revevar2 = HJConvert.ToString(dr["revevar2"]);
                    model.revevar3 = HJConvert.ToString(dr["revevar3"]);
                    model.revefloat1 = HJConvert.ToDecimal(dr["revefloat1"]);
                    model.revefloat2 = HJConvert.ToDecimal(dr["revefloat2"]);
                    model.revedate = HJConvert.ToDateTime(dr["revedate"]);

                    model.usermoney = HJConvert.ToDecimal(dr["usermoney"]);
                    model.reveint4 = HJConvert.ToInt32(dr["reveint4"]);
                    model.reveint5 = HJConvert.ToInt32(dr["reveint5"]);
                    model.reveint6 = HJConvert.ToInt32(dr["reveint6"]);
                    model.revevar4 = HJConvert.ToString(dr["revevar4"]);
                    model.revevar5 = HJConvert.ToString(dr["revevar5"]);
                    model.revefloat3 = HJConvert.ToDecimal(dr["revefloat3"]);
                    model.revefloat4 = HJConvert.ToDecimal(dr["revefloat4"]);

                }
            }
            return model;
        }

        /// <summary>
        /// 获得总记录数
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        public int GetCount(string strWhere)
        {
            return (int)SQLHelper.ExecuteScalar(CommandType.StoredProcedure, "pagecount", new SqlParameter("@tblName", "Deliver"), new SqlParameter("@strWhere", strWhere));
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pagesize">页尺寸</param>
        /// <param name="pageindex">页索引</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderName">排序字段</param>
        /// <param name="orderType">排序类型，1为降序，0为升序</param>
        /// <returns>图书列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<DeliverInfo> GetList(int pagesize, int pageindex, string strWhere, string orderName, int orderType)
        {
            IList<DeliverInfo> infos = new List<DeliverInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@tblName", SqlDbType.VarChar,255),
                new SqlParameter("@strGetFields", SqlDbType.VarChar,1000),
                new SqlParameter("@primary", SqlDbType.VarChar,255),
                new SqlParameter("@orderName", SqlDbType.VarChar,255),
                new SqlParameter("@PageSize", SqlDbType.Int),
                new SqlParameter("@PageIndex", SqlDbType.Int),
                new SqlParameter("@OrderType", SqlDbType.Bit),
                new SqlParameter("@strWhere", SqlDbType.VarChar,1500)
            };
            parameters[0].Value = "Deliver";
            parameters[1].Value = "*,(select cname from City where cid=Deliver.Inve1) cityname,(select classname from DeliverGroup where id=Deliver.GpsIMEI) Groupname";
            parameters[2].Value = "DataId";
            parameters[3].Value = orderName;
            parameters[4].Value = pagesize;
            parameters[5].Value = pageindex;
            parameters[6].Value = orderType;
            parameters[7].Value = strWhere;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "pageselectpri", parameters))
            {
                while (dr.Read())
                {
                    DeliverInfo info = new DeliverInfo();
                    info.DataId = HJConvert.ToInt32(dr["DataId"]);
                    info.CodeId = HJConvert.ToString(dr["CodeId"]);
                    info.Name = HJConvert.ToString(dr["Name"]);
                    info.Phone = HJConvert.ToString(dr["Phone"]);
                    info.Section = HJConvert.ToString(dr["Section"]);
                    info.Status = HJConvert.ToInt32(dr["Status"]);
                    info.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    info.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    info.UserName = HJConvert.ToString(dr["UserName"]);
                    info.Password = HJConvert.ToString(dr["Password"]);
                    info.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    info.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    info.AddDate = HJConvert.ToDateTime(dr["AddDate"]);
                    info.cityname = HJConvert.ToString(dr["cityname"]);
                    info.Groupname = HJConvert.ToString(dr["Groupname"]);
                    infos.Add(info);
                }
            }
            return infos;
        }

        /// <summary>
        /// 后台获取列表
        /// </summary>
        /// <param name="pagesize">页尺寸</param>
        /// <param name="pageindex">页索引</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderName">排序字段</param>
        /// <param name="orderType">排序类型，1为降序，0为升序</param>
        /// <returns>图书列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<DeliverInfo> GetDeliverList(int pagesize, int pageindex, string strWhere, string orderName, int orderType, string otherwhere)
        {
            IList<DeliverInfo> infos = new List<DeliverInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@pagesize", SqlDbType.Int),
                new SqlParameter("@pageindex", SqlDbType.Int),
                new SqlParameter("@orderfield", SqlDbType.VarChar,255),
                new SqlParameter("@ordertype", SqlDbType.VarChar,5),
                new SqlParameter("@where", SqlDbType.VarChar,1500),
                new SqlParameter("@otherwhere", SqlDbType.VarChar,1500)
            };
            parameters[0].Value = pagesize;
            parameters[1].Value = pageindex;
            parameters[2].Value = orderName;
            parameters[3].Value = orderType == 1 ? "desc" : "asc"; ;
            parameters[4].Value = strWhere;
            parameters[5].Value = otherwhere;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "deliver_getlistWithState", parameters))
            {
                while (dr.Read())
                {
                    DeliverInfo info = new DeliverInfo();
                    info.DataId = HJConvert.ToInt32(dr["DataId"]);
                    info.CodeId = HJConvert.ToString(dr["CodeId"]);
                    info.Name = HJConvert.ToString(dr["Name"]);
                    info.Phone = HJConvert.ToString(dr["Phone"]);
                    info.Section = HJConvert.ToString(dr["Section"]);

                    DateTime lasttime = HJConvert.ToDateTime(dr["lasttime"]);
                    if ((DateTime.Now - lasttime).TotalMinutes < Hangjing.Common.Constant.OffLineDoor)
                    {
                        info.Status = 1;
                    }

                    info.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    info.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    info.UserName = HJConvert.ToString(dr["UserName"]);
                    info.Password = HJConvert.ToString(dr["Password"]);
                    info.Inve1 = HJConvert.ToInt32(dr["Inve1"]);

                    info.cartype = HJConvert.ToString(dr["cartype"]);
                    info.carower = HJConvert.ToInt32(dr["carower"]);




                    info.recordtcount = HJConvert.ToInt32(dr["recordtcount"]);
                    info.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    info.AddDate = HJConvert.ToDateTime(dr["AddDate"]);
                    info.cityname = HJConvert.ToString(dr["cityname"]);
                    info.Groupname = HJConvert.ToString(dr["Groupname"]);

                    info.reveint1 = HJConvert.ToInt32(dr["reveint1"]);
                    info.reveint2 = HJConvert.ToInt32(dr["reveint2"]);
                    info.reveint3 = HJConvert.ToDecimal(dr["reveint3"]);
                    info.revevar3 = HJConvert.ToString(dr["revevar3"]);



                    info.deluser = HJConvert.ToString(dr["deluser"]);
                    info.deltime = HJConvert.ToDateTime(dr["deltime"]);

                    infos.Add(info);
                }
            }
            return infos;
        }




        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="Id"></param>
        /// 此代码由杭景科技代码内部生成器自动生成
        /// <returns></returns>
        public int DelDeliver(int Id)
        {
            return DelList(Id.ToString());
        }


        /// <summary>
        /// 批量删除记录
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        /// 此代码由杭景科技代码内部生成器自动生成
        public int DelList(string IdList)
        {
            StringBuilder str = new StringBuilder();
            str.Append("delete from Deliver where DataId in ({0})");

            return SQLHelper.ExecuteNonQuery(CommandType.Text, string.Format(str.ToString(), IdList), null);
        }

        /// <summary>
        /// 删除或者恢复
        /// </summary>
        /// <param name="IdList"></param>
        /// <param name="adminname"></param>
        /// <param name="delflag">0表示恢复，1表示删除</param>
        /// <returns></returns>
        public int DelOrRecoverDeliver(string IdList, string adminname, int delflag)
        {
            StringBuilder str = new StringBuilder();
            //删除其他
            str.Append("UPDATE dbo.Deliver SET delflag = " + delflag + ",deluser = '" + adminname + "',deltime=getdate()  where dataid in ({0});");

            return SQLHelper.ExecuteNonQuery(CommandType.Text, string.Format(str.ToString(), IdList), null);
        }


        /// <summary>
        /// 物理删除
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        /// 此代码由杭景科技代码内部生成器自动生成
        public int CompletelyDelList(string IdList)
        {
            StringBuilder str = new StringBuilder();

            //删除其他
            str.Append("delete from Deliver where dataid in ({0});");

            return SQLHelper.ExecuteNonQuery(CommandType.Text, string.Format(str.ToString(), IdList), null);
        }

        /// <summary>
        /// 获取记录所有字段
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        /// <param>DataId</param>
        /// <returns>DeliverInfo</returns>
        public DeliverInfo GetModelByCodeId(string CodeId)
        {
            string sql = "selectDataId,CodeId,Name,Phone,Section,Status,GpsIMEI,OrderNum,UserName,Password,Inve1,Inve2,AddDate from Deliver where  CodeId = @CodeId";
            SqlParameter parameter = new SqlParameter("@CodeId", SqlDbType.VarChar, 10);
            parameter.Value = CodeId;

            DeliverInfo model = null;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, parameter))
            {
                if (dr.Read())
                {
                    model = new DeliverInfo();
                    model.DataId = HJConvert.ToInt32(dr["DataId"]);
                    model.CodeId = HJConvert.ToString(dr["CodeId"]);
                    model.Name = HJConvert.ToString(dr["Name"]);
                    model.Phone = HJConvert.ToString(dr["Phone"]);
                    model.Section = HJConvert.ToString(dr["Section"]);
                    model.Status = HJConvert.ToInt32(dr["Status"]);
                    model.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    model.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    model.UserName = HJConvert.ToString(dr["UserName"]);
                    model.Password = HJConvert.ToString(dr["Password"]);
                    model.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    model.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    model.AddDate = HJConvert.ToDateTime(dr["AddDate"]);
                }
            }
            return model;
        }

        /// <summary>
        /// 获取记录所有字段
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        /// <param>DataId</param>
        /// <returns>DeliverInfo</returns>
        public DeliverInfo GetModelByIMEI(string IMEI)
        {
            string sql = "select DataId,CodeId,Name,Phone,Section,Status,GpsIMEI,OrderNum,UserName,Password,Inve1,Inve2,AddDate from Deliver where  GpsIMEI = @GpsIMEI";
            SqlParameter parameter = new SqlParameter("@GpsIMEI", SqlDbType.VarChar, 10);
            parameter.Value = IMEI;

            DeliverInfo model = null;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, parameter))
            {
                if (dr.Read())
                {
                    model = new DeliverInfo();
                    model.DataId = HJConvert.ToInt32(dr["DataId"]);
                    model.CodeId = HJConvert.ToString(dr["CodeId"]);
                    model.Name = HJConvert.ToString(dr["Name"]);
                    model.Phone = HJConvert.ToString(dr["Phone"]);
                    model.Section = HJConvert.ToString(dr["Section"]);
                    model.Status = HJConvert.ToInt32(dr["Status"]);
                    model.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    model.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    model.UserName = HJConvert.ToString(dr["UserName"]);
                    model.Password = HJConvert.ToString(dr["Password"]);
                    model.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    model.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    model.AddDate = HJConvert.ToDateTime(dr["AddDate"]);
                }
            }
            return model;
        }

        /// <summary>
        /// 配送员登录 zjf@ihangjing.com 2012.5.5 add
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public DeliverInfo GetModelByUserNameAndPassword(string UserName, string Password)
        {
            SqlParameter[] parameters =
            {
                new SqlParameter("@UserName" , SqlDbType.VarChar , 50),
                new SqlParameter("@Password" , SqlDbType.VarChar ,50)
            };
            parameters[0].Value = UserName;
            parameters[1].Value = Password;

            DeliverInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "Deliver_httplogin", parameters))
            {
                if (dr.Read())
                {
                    model = new DeliverInfo();

                    model.DataId = HJConvert.ToInt32(dr["DataId"]);
                    model.CodeId = HJConvert.ToString(dr["CodeId"]);
                    model.Name = HJConvert.ToString(dr["Name"]);
                    model.Phone = HJConvert.ToString(dr["Phone"]);
                    model.Section = HJConvert.ToString(dr["Section"]);
                    model.Status = HJConvert.ToInt32(dr["Status"]);
                    model.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    model.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    model.UserName = HJConvert.ToString(dr["UserName"]);
                    model.Password = HJConvert.ToString(dr["Password"]);
                    model.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    model.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    model.AddDate = HJConvert.ToDateTime(dr["AddDate"]);

                }
            }

            return model;
        }

        /// <summary>
        /// 修改用户密码 zjf@ihangjing.com 2012.5.7
        /// </summary>
        /// <param name="dataid"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public int ChangePassword(int Dataid, string Password)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Deliver set Password=@Password where dataid=@dataid");
            SqlParameter[] parameters =
            {
                new SqlParameter("@Password" ,SqlDbType.VarChar , 50),
                new SqlParameter("@dataid" , SqlDbType.Int , 4)
            };
            parameters[0].Value = Password;
            parameters[1].Value = Dataid;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取列表 包含配送员有多少订单在配送中
        /// </summary>
        /// <param name="pagesize">页尺寸</param>
        /// <param name="pageindex">页索引</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderName">排序字段</param>
        /// <param name="orderType">排序类型，1为降序，0为升序</param>
        /// <returns>图书列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<DeliverInfo> GetListWithOrderNum(int pagesize, int pageindex, string strWhere, string orderName, int orderType)
        {
            IList<DeliverInfo> infos = new List<DeliverInfo>();


            SqlParameter[] parameters =
            {
                new SqlParameter("@where", SqlDbType.VarChar,1500)
            };
            parameters[0].Value = strWhere;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "deliver_ListWithOrderNum", parameters))
            {
                while (dr.Read())
                {
                    DeliverInfo info = new DeliverInfo();
                    info.DataId = HJConvert.ToInt32(dr["DataId"]);
                    info.CodeId = HJConvert.ToString(dr["CodeId"]);
                    info.Name = HJConvert.ToString(dr["Name"]);
                    info.Phone = HJConvert.ToString(dr["Phone"]);
                    info.Section = HJConvert.ToString(dr["Section"]);
                    info.Status = HJConvert.ToInt32(dr["Status"]);
                    info.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    info.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    info.UserName = HJConvert.ToString(dr["UserName"]);
                    info.Password = HJConvert.ToString(dr["Password"]);
                    info.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    info.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    info.AddDate = HJConvert.ToDateTime(dr["lasttime"]);

                    if (dr["lasttime"] is System.DBNull)
                    {
                        info.AddDate = HJConvert.ToDateTime("1970-1-1");
                    }
                    else
                    {
                        info.AddDate = HJConvert.ToDateTime(dr["lasttime"]);

                    }


                    info.OrderNum = HJConvert.ToInt32(dr["TOrderNum"]);
                    info.processcount = HJConvert.ToInt32(dr["processcount"]);


                    info.workstate = HJConvert.ToInt32(dr["workstate"]);

                    info.Lat = HJConvert.ToString(dr["lat"]);
                    info.Lng = HJConvert.ToString(dr["Lng"]);

                    if (info.Lat != "" && info.Lng != "")
                    {
                        info.speed = 0;
                        try
                        {
                            info.speed = HJConvert.ToInt32(HJConvert.ToDouble(dr["speed"]));
                        }
                        catch (Exception)
                        {
                            AppLog.AppLog.Error("speed 转化出错:" + HJConvert.ToString(dr["speed"]));
                        }
                        info.direction = HJConvert.ToInt32(HJConvert.ToDouble(dr["direction"]));
                        info.carstate = "";

                        DateTime recordAddTime = HJConvert.ToDateTime(dr["lasttime"]);

                        //大于5分没上传数据，表示离线
                        if (DateTime.Now > recordAddTime.AddMinutes(5))
                        {
                            info.carstate = "离线";
                        }
                        else
                        {
                            if (info.speed == 0)
                            {
                                info.carstate = "停车";
                            }
                            else
                            {
                                info.carstate = "行驶(" + info.speed + "公里/小时)";
                            }
                        }

                    }
                    else
                    {
                        info.Lat = "0";
                        info.Lng = "0";
                        info.speed = 0;
                        info.direction = 0;
                        info.carstate = "离线";
                    }


                    infos.Add(info);
                }
            }
            return infos;
        }

        /// <summary>
        /// 商家获取附近配送员
        /// </summary>
        /// <param name="lat"></param>
        /// <param name="lng"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public IList<DeliverInfo> shopgetDelivers(string lat, string lng, string strWhere)
        {
            IList<DeliverInfo> infos = new List<DeliverInfo>();


            SqlParameter[] parameters =
            {
                new SqlParameter("@lat", SqlDbType.VarChar,50),
                new SqlParameter("@lng", SqlDbType.VarChar,50),
                new SqlParameter("@where", SqlDbType.VarChar,1500)
            };
            parameters[0].Value = lat;
            parameters[1].Value = lng;
            parameters[2].Value = strWhere;


            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "deliver_shopgetDelivers", parameters))
            {
                while (dr.Read())
                {
                    DeliverInfo info = new DeliverInfo();
                    info.DataId = HJConvert.ToInt32(dr["DataId"]);

                    info.Name = HJConvert.ToString(dr["Name"]);
                    info.Phone = HJConvert.ToString(dr["Phone"]);
                    info.workstate = HJConvert.ToInt32(dr["workstate"]);
                    info.Lat = HJConvert.ToString(dr["lat"]);
                    info.Lng = HJConvert.ToString(dr["Lng"]);

                    info.distance = Convert.ToInt32(HJConvert.ToDecimal(dr["distance"]) * 1000);

                    info.processcount = HJConvert.ToInt32(dr["processcount"]);

                    infos.Add(info);
                }
            }
            return infos;
        }


        /// <summary>
        /// 获取一个配送员
        /// </summary>
        ///此代码由杭景科技代码内部生成器自动生成
        public DeliverInfo GetOneDeliver(int did)
        {
            DeliverInfo info = null;
            string sql = " select top 1 a.*,b.*,(select count(*) ordernum from Custorder where  deliverid=a.DataId and OrderStatus=7 and sendstate = 0 ) as TOrderNum from Deliver a left join GPS_Records b on a.dataid = b.jh1 where a.dataid = @did order by ID desc ";

            SqlParameter[] parameters =
            {
                new SqlParameter("@did", SqlDbType.Int)
            };
            parameters[0].Value = did;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, parameters))
            {
                while (dr.Read())
                {
                    info = new DeliverInfo();
                    info.DataId = HJConvert.ToInt32(dr["DataId"]);
                    info.CodeId = HJConvert.ToString(dr["CodeId"]);
                    info.Name = HJConvert.ToString(dr["Name"]);
                    info.Phone = HJConvert.ToString(dr["Phone"]);
                    info.Section = HJConvert.ToString(dr["Section"]);
                    info.Status = HJConvert.ToInt32(dr["Status"]);
                    info.GpsIMEI = HJConvert.ToString(dr["GpsIMEI"]);
                    info.OrderNum = HJConvert.ToInt32(dr["OrderNum"]);
                    info.UserName = HJConvert.ToString(dr["UserName"]);
                    info.Password = HJConvert.ToString(dr["Password"]);
                    info.Inve1 = HJConvert.ToInt32(dr["Inve1"]);
                    info.Inve2 = HJConvert.ToString(dr["Inve2"]);
                    info.AddDate = HJConvert.ToDateTime(dr["AddDate"]);
                    //获取配送员正在配送的订单个数
                    info.OrderNum = HJConvert.ToInt32(dr["TOrderNum"]);
                    //坐标信息要修正
                    string Lat = HJConvert.ToString(dr["jh3"]);
                    string Lng = HJConvert.ToString(dr["jh2"]);
                    if (Lat != "" && Lng != "")
                    {
                        info.Lat = Lat;
                        info.Lng = Lng;

                        info.speed = HJConvert.ToInt32(Convert.ToDecimal(dr["JH4"]));
                        info.direction = HJConvert.ToInt32(Convert.ToDecimal(dr["JH5"]));
                        info.carstate = "";

                        DateTime recordAddTime = HJConvert.ToDateTime(dr["AddTime"]);

                        //大于5分没上传数据，表示离线
                        if (DateTime.Now > recordAddTime.AddMinutes(5))
                        {
                            info.carstate = "离线";
                        }
                        else
                        {
                            if (info.speed == 0)
                            {
                                info.carstate = "停车";
                            }
                            else
                            {
                                info.carstate = "行驶(" + info.speed + "公里/小时)";
                            }
                        }

                    }
                    else
                    {
                        info.Lat = "";
                        info.Lng = "";
                        info.speed = 0;
                        info.direction = 0;
                        info.carstate = "离线";
                    }

                }
            }
            return info;
        }

        /// <summary>
        ///  配送员重新分配后保存[群编号未修改的，不编辑]
        /// </summary>
        /// <param name="list">有修改的列表</param>
        /// <param name="nosetlist">设置为未分配的，群编号设置为0</param>
        /// <returns></returns>
        public bool SaveGroupDeliverR(IList<Hangjing.Model.DeliverGroupInfo> list, IList<Hangjing.Model.DeliverInfo> nosetlist)
        {
            IList<ROrderinfo> my_list = new List<ROrderinfo>();
            ArrayList cmdtextlist = new ArrayList();
            ArrayList paraslist = new ArrayList();

            DeliverGroupInfo model = new DeliverGroupInfo();
            model.ID = 0;
            model.deliverlst = nosetlist;
            list.Add(model);

            foreach (var item in list)
            {
                foreach (var shop in item.deliverlst)
                {
                    if (shop.GpsIMEI != item.ID.ToString())
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update Deliver set ");

                        strSql.Append("GpsIMEI=@GpsIMEI");
                        strSql.Append(" where DataId=@DataId");
                        SqlParameter[] parameters =
                        {
                            new SqlParameter("@GpsIMEI", SqlDbType.Int,4) ,
                            new SqlParameter("@DataId", SqlDbType.Int,4) ,
                        };
                        parameters[0].Value = item.ID;
                        parameters[1].Value = shop.DataId;

                        cmdtextlist.Add(strSql.ToString());
                        paraslist.Add(parameters);
                    }

                }
            }



            return SQLHelper.ExecuteSqlTran(CommandType.Text, cmdtextlist, paraslist);

        }


        /// <summary>
        /// 更新app登入信息,登录时设置1，退出设置0
        /// </summary>
        /// <param name="TogoNum"></param>
        /// <param name="NewPwd"></param>
        /// <returns></returns>
        public int UpdateLoginState(int TogoNum, int State)
        {
            StringBuilder str = new StringBuilder();
            str.Append("update Deliver set Status=@State where DataID=@TogoNum");

            SqlParameter[] Para =
            {
                new SqlParameter("@TogoNum",SqlDbType.Int),
                new SqlParameter("@State",SqlDbType.Int)
            };
            Para[0].Value = TogoNum;
            Para[1].Value = State;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }

        /// <summary>
        /// 查询附近几公里内的配送员（在线）
        /// </summary>
        /// <param name="distance">范围</param>
        /// <param name="lat">纬度</param>
        /// <param name="lng">经度</param>
        /// <returns></returns>
        public IList<DeliverInfo> GetNearbyList(decimal distance, string lat, string lng)
        {
            IList<DeliverInfo> infos = new List<DeliverInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@distance", SqlDbType.Decimal),
                new SqlParameter("@lat", SqlDbType.VarChar,50),
                new SqlParameter("@lng", SqlDbType.VarChar,50),
            };
            parameters[0].Value = distance;
            parameters[1].Value = lat;
            parameters[2].Value = lng;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "deliver_Nearby", parameters))
            {
                while (dr.Read())
                {
                    DeliverInfo info = new DeliverInfo();
                    info.DataId = HJConvert.ToInt32(dr["DataId"]);
                    info.Name = HJConvert.ToString(dr["Name"]);
                    info.Lat = HJConvert.ToString(dr["lat"]);
                    info.Lng = HJConvert.ToString(dr["lng"]);
                    infos.Add(info);
                }
            }
            return infos;
        }

        /// <summary>
        /// 查询附近几公里内的配送员（在线待岗）
        /// </summary>
        /// <param name="distance">范围</param>
        /// <param name="lat">纬度</param>
        /// <param name="lng">经度</param>
        /// <returns></returns>
        public IList<DeliverInfo> getNearbyOnlineIdle(decimal distance, string lat, string lng)
        {
            IList<DeliverInfo> infos = new List<DeliverInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@distance", SqlDbType.Decimal),
                new SqlParameter("@lat", SqlDbType.VarChar,50),
                new SqlParameter("@lng", SqlDbType.VarChar,50),
            };
            parameters[0].Value = distance;
            parameters[1].Value = lat;
            parameters[2].Value = lng;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "deliver_getNearbyOnlineIdle", parameters))
            {
                while (dr.Read())
                {
                    DeliverInfo info = new DeliverInfo();
                    info.DataId = HJConvert.ToInt32(dr["DataId"]);
                    info.Name = HJConvert.ToString(dr["Name"]);
                    info.Lat = HJConvert.ToString(dr["lat"]);
                    info.Lng = HJConvert.ToString(dr["lng"]);
                    infos.Add(info);
                }
            }
            return infos;
        }


        /// <summary>
        /// 订单统计
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <param name="strWhere"></param>
        /// <param name="orderwhere"></param>
        /// <returns></returns>
        public IList<TogoInfo> SendOrderStatistics(int pagesize, int pageindex, string strWhere, string orderwhere, string ordername, int daycount, string otherwhere)
        {
            IList<TogoInfo> infos = new List<TogoInfo>();
            SqlParameter[] parameters =
            {
                new SqlParameter("@PageSize", SqlDbType.Int),
                new SqlParameter("@PageIndex", SqlDbType.Int),
                new SqlParameter("@strWhere", SqlDbType.VarChar,1500),
                new SqlParameter("@orderwhere", SqlDbType.VarChar,1500),
                new SqlParameter("@ordername", SqlDbType.VarChar,100),
                new SqlParameter("@daycount", SqlDbType.Int),
                new SqlParameter("@otherwhere", SqlDbType.VarChar,1500),
            };
            parameters[0].Value = pagesize;
            parameters[1].Value = pageindex;
            parameters[2].Value = strWhere;
            parameters[3].Value = orderwhere;
            parameters[4].Value = ordername;
            parameters[5].Value = daycount;
            parameters[6].Value = otherwhere;

            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "deliver_SendOrderStatistics", parameters))
            {
                while (dr.Read())
                {
                    TogoInfo info = new TogoInfo();
                    info.TogoName = HJConvert.ToString(dr["classname"]);
                    info.allcount = HJConvert.ToInt32(dr["ordercount"]);
                    info.allprice = HJConvert.ToDecimal(dr["TotalPrice"]);


                    info.Advance = HJConvert.ToDecimal(dr["Advance"]);


                    info.ShopHaveMoney = HJConvert.ToDecimal(dr["OrderTotal"]);
                    info.DataID = HJConvert.ToInt32(dr["recordtcount"]);
                    info.Shopprofit = HJConvert.ToDecimal(dr["Shopprofit"]);


                    info.Commission = HJConvert.ToDecimal(dr["Commission"]);


                    info.EMail = Utils.second2MinAndSecond(HJConvert.ToInt32(dr["orderusetime"]));
                    info.Address = Utils.second2MinAndSecond(HJConvert.ToInt32(dr["sendtime"]));
                    info.Grade = HJConvert.ToInt32(dr["post"]);

                    info.Fetion = HJConvert.ToString(dr["GpsIMEI"]);//分站编号

                    info.IsDelete = HJConvert.ToInt32(dr["Unid"]);
                    info.cardpay = HJConvert.ToDecimal(dr["cardpay"]);

                    info.timeoutcount = HJConvert.ToInt32(dr["timeoutcount"]);

                    info.delivergetfee_base = HJConvert.ToDecimal(dr["delivergetfee_base"]);
                    info.DeliverGetSendFee = HJConvert.ToDecimal(dr["DeliverGetSendFee"]);


                    infos.Add(info);
                }
            }
            return infos;
        }

        /// <summary>
        /// 初始化配送员状态
        /// </summary>
        /// <param name="deliverid"></param>
        public void InitWorkState(int deliverid)
        {
            //如果这个配送员手上没有配送员的订单，状态设置待岗
            string sql = " delflag = 0 and  OrderStatus = 7  and  deliverid = " + deliverid;
            IList<CustorderInfo> orders = new Custorder().DeliverProcessOrders(deliverid);
            int workstate = 10;
            if (orders.Count == 0)
            {
                workstate = 10;
            }
            else
            {
                foreach (var item in orders)
                {
                    if (item.sendstate == 1)
                    {
                        workstate = 20;
                    }
                    if (item.sendstate == 2)
                    {
                        workstate = 30;
                        break;
                    }
                }
            }

            sql = "UPDATE dbo.Deliver SET workstate = " + workstate + " where dataid = " + deliverid;
            SQLHelper.excutesql(sql);

        }

    }
}
